package com.promfy.timetable.db;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.promfy.timetable.domain.OneClass;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class OneClassDBService {
	private static final String TAG = "mt_dbsv";

	private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
	private OneClassDBOpenHelper dbHelper;
	private SQLiteDatabase db;
	private ContentResolver resolver;
	public OneClassDBService(Context context) {
		dbHelper = new OneClassDBOpenHelper(context);
	}

	/**
	 * get all syslog
	 */
	public List<OneClass> getList()  throws Exception{
		db = dbHelper.getWritableDatabase();
		List<OneClass> oneClassList = new ArrayList<OneClass>();
		String sql = "SELECT  _id, week, course, className, location, teacher, prop FROM classinfo";
		Cursor cursor = null;
		try {
			cursor = db.rawQuery(sql, null);
			while (cursor.moveToNext()) {
			
				OneClass oneClass = new OneClass(
						cursor.getInt(cursor.getColumnIndex("_id")),
						cursor.getInt(cursor.getColumnIndex("week")), 
						cursor.getInt(cursor.getColumnIndex("course")), 
						cursor.getString(cursor.getColumnIndex("className")), 
						cursor.getString(cursor.getColumnIndex("location")), 
						cursor.getString(cursor.getColumnIndex("teacher")), 
						cursor.getInt(cursor.getColumnIndex("prop")));
				oneClassList.add(oneClass);
			}
		}catch (Exception e) {
			e.printStackTrace();
		} 
		finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		return oneClassList; 
	}

	
	

	/**
	 * insert data to db
	 * @param task
	 * @throws Exception
	 */
	public void insert(OneClass oneClass) throws Exception{
		db = dbHelper.getWritableDatabase();
		try {
			ContentValues cv = new ContentValues();
			cv.put("week", oneClass.getWeek());
			cv.put("course", oneClass.getCourse());
			cv.put("className", oneClass.getClassName());
			cv.put("location", oneClass.getLocation());
			cv.put("teacher", oneClass.getTeacher());
			cv.put("prop", oneClass.getProp());
			db.insert("classinfo", null, cv);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	/**
	 * insert data to db
	 * @param task
	 * @throws Exception
	 */
	public void update(OneClass oneClass) throws Exception{
		db = dbHelper.getWritableDatabase();
		try {
			ContentValues cv = new ContentValues();
			cv.put("week", oneClass.getWeek());
			cv.put("course", oneClass.getCourse());
			cv.put("className", oneClass.getClassName());
			cv.put("location", oneClass.getLocation());
			cv.put("teacher", oneClass.getTeacher());
			cv.put("prop", oneClass.getProp());
			db.update("classinfo", cv, "_id = ?", new String[]{oneClass.getId()+""});
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public static String getCurrDate() {
		Calendar c = Calendar.getInstance();
		Date date = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
		String dStr = sdf.format(date);
		return dStr;
	}
	public static String getCurrDate(long time) {
		
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
		String dStr = sdf.format(new Date(time));
		return dStr;
	}
	
	public static String getComparedFormateDateString(long time) {
		String DATE_FORMAT_PATTERN1 = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN1);
		String dStr = sdf.format(new Date(time));
		return dStr;
	}
}
